package com.example.basic.web.service.basicDataValue.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.base.impl.BaseEntityServiceImpl;
import com.example.basic.exception.ServiceException;
import com.example.basic.utils.PinyinUtils;
import com.example.basic.utils.StringUtils;
import com.example.basic.utils.WubiUtils;
import com.example.basic.web.mapper.basicDataValue.BasicDataValueMapper;
import com.example.basic.web.model.basicDataValue.BasicDataValue;
import com.example.basic.web.service.basicData.BasicDataService;
import com.example.basic.web.service.basicDataValue.BasicDataValueService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static com.example.basic.utils.CheckUtil.notNull;


/**
 * <p>
 * 基础数据值记录 服务实现类
 * </p>
 *
 * @author zhangy
 * @since 2020-5-25
 */
@Service
@Transactional
public class BasicDataValueServiceImpl extends BaseEntityServiceImpl<BasicDataValueMapper, BasicDataValue> implements BasicDataValueService {

    @Resource
    private BasicDataValueMapper basicDataValueMapper;
    @Resource
    private BasicDataService basicDataService;

    /**
     * 新增修改配置内容
     * @param basicDataValue
     * @return
     */
    @Override
    public BasicDataValue addOrUpdate(BasicDataValue basicDataValue) {
        notNull(basicDataValue,"配置内容实体不能为空!");
        notNull(basicDataValue.getBasicCode(),"配置管理主代码不能为空!");
        notNull(basicDataValue.getValue(),"配置内容值不能为空!");
        notNull(basicDataValue.getValueRemark(),"配置内容值含义不能为空!");

        //校验值唯一
        LambdaQueryWrapper<BasicDataValue> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BasicDataValue::getValue,basicDataValue.getValue());
        lqw.eq(BasicDataValue::getBasicCode,basicDataValue.getBasicCode());
        if(StringUtils.notEmptyParams(basicDataValue.getId())){
            lqw.ne(BasicDataValue::getId,basicDataValue.getId());
        }
        BasicDataValue localCode = this.mapper.selectOne(lqw);

        if(localCode != null ){
            throw new ServiceException("配置内容值已存在");
        }

        //校验值含义唯一
        lqw.eq(BasicDataValue::getValueRemark,basicDataValue.getValueRemark());
        lqw.eq(BasicDataValue::getBasicCode,basicDataValue.getBasicCode());
        if(StringUtils.notEmptyParams(basicDataValue.getId())){
            lqw.ne(BasicDataValue::getId,basicDataValue.getId());
        }
        BasicDataValue localRemark = this.mapper.selectOne(lqw);

        if(localRemark != null ){
            throw new ServiceException("配置内容值含义已存在");
        }

        //生成拼音码，五笔码
        generatePyWbCode(basicDataValue);

        if(StringUtils.notEmptyParams(basicDataValue.getId())){
            preUpdate(basicDataValue);
        }else {
            preCreate(basicDataValue);
        }

        save(basicDataValue);
        return basicDataValue;
    }

    /**
     * @create 生成五笔码、拼音码
     * @param basicDataValue
     */
    private void generatePyWbCode(BasicDataValue basicDataValue) {
        notNull(basicDataValue, "参数不能为空！");

        // 获取信息
        String chronicName = StringUtils.safeToString(basicDataValue.getValueRemark(), "");

        // 生成拼音码
        if("".equals(StringUtils.safeToString(basicDataValue.getPyCode(),"").replaceAll(" ",""))) {
            String chronicNamePinyin = PinyinUtils.getSimplePinYin(chronicName);
            basicDataValue.setPyCode(chronicNamePinyin);
        }

        // 生成五笔码
        if("".equals(StringUtils.safeToString(basicDataValue.getWbCode(),"").replaceAll(" ",""))) {
            String chronicNameWubi = WubiUtils.getSimpleWuBi(chronicName);
            basicDataValue.setWbCode(chronicNameWubi);
        }
    }

    /**
     * 删除基础数据值记录
     * @param basicCode 配置管理主代码
     * @param value 值
     */
    @Override
    public boolean remove(String basicCode,String value) {
        notNull(basicCode,"配置管理主代码不能为空!");

        //查询数据库中数据
        BasicDataValue basicDataValue = this.detail(basicCode, value);

        //判断是否存在
        if(basicDataValue == null){
            throw new ServiceException("未查询到当前数据，请确认!");
        }

        //物理删除数据
        basicDataValueMapper.remove(basicCode,value);

        return true;
    }

    /**
     * 查询基础数据值记录
     * @param basicCode 配置管理主代码
     * @param value 值
     */
    @Override
    public BasicDataValue detail(String basicCode, String value) {
        notNull(basicCode,"配置管理主代码不能为空!");
        notNull(value,"值不能为空!");

        LambdaQueryWrapper<BasicDataValue> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BasicDataValue::getValue,value);
        lqw.eq(BasicDataValue::getBasicCode,basicCode);

        return this.mapper.selectOne(lqw);
    }

    /**
     * 查询基础数据值记录列表
     * @param searchParam 模糊搜索（拼音、五笔、名称、代码）
     * @param basicCode 配置管理主代码
     * @param page 页码
     * @param size 数量
     * @return com.ruiyicloud.core.result.Result
     */
    @Override
    public IPage<BasicDataValue> queryList(Integer page, Integer size, String basicCode, String searchParam) {
        notNull(basicCode,"配置管理主代码不能为空!");

        //分页
        Page<BasicDataValue> basicDataValuePage = new Page<>(page, size);
        return basicDataValueMapper.queryList(basicDataValuePage,basicCode,searchParam);
    }

    /**
     * 删除配置内容
     * @param basicCode 配置管理主代码
     * @return
     */
    @Override
    public Boolean removeByBasicCode(String basicCode) {
        notNull(basicCode,"配置管理主代码不能为空!");
        return basicDataValueMapper.removeByBasicCode(basicCode);
    }
}
